import { request, storage } from '@doctorwork/saga';
import {delay} from '@doctorwork/utils';

export default {
    namespace: 'address',
    state: {
        inited: false,
        list: [],
        selected: null
    },
    reducers: {
        save: (state, { payload }) => {
            return {
                ...state,
                inited: true,
                list: payload.list.map(item => {
                    item.address = `${item.provinceName}${item.cityName}${
                        item.districtName
                    }`;
                    return item;
                })
            };
        }
    },
    effects: {
        /**
         * 取数据
         * @param {*} _action
         * @param {*} param1
         */
        *list(_action, { put }) {
            const list = yield put.resolve(
                request.get({
                    url: '/api/user/address/query'
                })
            );
            if (!list) {
                return;
            }
            yield put({
                type: 'save',
                payload: {
                    list
                }
            });
        },
        /**
         * 删除
         * @param {*} _action
         * @param {*} param1
         */
        *delete({ payload }, { put }) {
            const data = yield put.resolve({
                type: '$request/post',
                payload: {
                    url: '/api/user/address/delete',
                    data: {
                        id: payload.id
                    }
                }
            });
            const list = yield put.resolve(
                request.get({
                    url: '/api/user/address/query'
                })
            );
            yield put({
                type: 'save',
                payload: {
                    list
                }
            });
        },
        /**
         * 修改默认配置
         * @param {*} _action
         * @param {*} param1
         */
        *changeDefalut({ payload }, { put }) {
            const data = yield put.resolve({
                type: '$request/post',
                payload: {
                    url: '/api/user/address/update',
                    data: {
                        ...payload,
                        isDefault: 1
                    }
                }
            });
            const list = yield put.resolve(
                request.get({
                    url: '/api/user/address/query'
                })
            );
            yield put({
                type: 'save',
                payload: {
                    list
                }
            });
        },
        /**
         * 确认收货地址
         * @param {*} param0
         * @param {*} param1
         */
        *saveAddress({ payload }, { put, call }) {
            yield put.resolve(storage.set('userAddress', payload));
            yield call(delay, 500);
            const address = yield put.resolve(storage.get('userAddress'));
            yield put.resolve({
                type: 'goodsOrder/save',
                payload: {
                    currentAddressId: address.id || '',
                    userAddress: address,
                    inputValue: address.identityCard || ''
                }
            })
            yield put({
                type: '$router/back',
                payload: 1
            });
        }
    }
};
